package servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.*;

@WebServlet("/CollegeServlet")
public class CollegeServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try{
            Class.forName("com.mysql.jdbc.Driver");
            String url="jdbc:mysql://localhost:3306/epidemic";
            String user="root";
            String pwd="WJHwenjiahui245";
            Connection connection= DriverManager.getConnection(url,user,pwd);
            Statement stmt =connection.createStatement();
            String sql ="select * from epidemic";
            ResultSet rs=stmt.executeQuery(sql);
            req.setAttribute("rs",rs);
            req.getRequestDispatcher("user.html").forward(req,resp);
        }catch (ClassNotFoundException | SQLException e){
            e.printStackTrace();
        }
        resp.getWriter().print("查询失败");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
